package com.zlm.demo1;

/**
 * @Author: 郑
 * 使用synchronized锁实现买票
 */
public class SaleTicketDemo2 {
    public static void main(String[] args) {
        Ticket2 ticket2 = new Ticket2();
        new Thread(() ->{
            for (int i = 0; i < 50; i++) {
                ticket2.sale();
            }
        },"a").start();
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                ticket2.sale();
            }
        },"b").start();
    }
}

class Ticket2{
    // 属性方法
    private int num = 50;

    //买票
    public synchronized void sale(){
        if(num > 0){
            System.out.println(Thread.currentThread().getName()+"卖出了"+(51 - num--)+"张票，还有"+num+"张票");
        }
    }
}
